package com.cn.indentManage.controller;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.indentManage.entity.Page;
import com.cn.indentManage.service.IndentService;



@Controller
@RequestMapping("/indent")
public class IndentController {
	@Resource
	private IndentService indentService;
	
	/*
	 * 主页
	 */
	@RequestMapping("/index")
	public String toIndex(HttpServletRequest req,HttpServletResponse res){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		return "index/index";
	}
	/*
	 * 查询订单
	 */
	@RequestMapping("/toIndent")
	public String toIndent(HttpServletRequest req,HttpServletResponse res,Model model,int currentPage){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		Page page = indentService.selectAllIndents(model, currentPage);
		model.addAttribute("page",page);
		return "orders/indent";
	}
	
	//通过订单状态来筛选订单
	@RequestMapping("/toIndentScreen")
	public String toIndentScreen(HttpServletRequest req,HttpServletResponse res,Model model,int currentPage,Integer status){
		res.setHeader("Access-Control-Allow-Origin", "*");  
		res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
		res.setHeader("Access-Control-Max-Age", "3600");  
		res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		Page page = indentService.selectAllIndentbystatus(model,currentPage,status);
		model.addAttribute("page",page);
		model.addAttribute("status", status);
		return "orders/indent";
	}
	/*
	 * 查看订单详情
	 */
		
	@RequestMapping("/toIndentXq")
	public String toIndentXq(HttpServletRequest req,HttpServletResponse res,Model model,int indentId){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	    List<Map<String, Object>> list = indentService.selectIndentXq(indentId,model);
	    model.addAttribute("list",list);
		return "orders/indentXq";
	}
	/*
	 * 修改订单状态
	 */
	@RequestMapping("/upStatus")
	@ResponseBody
	public String upStatus(HttpServletResponse res,int indentId,int status){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	    try{
	    	indentService.upIndentByindentId(indentId, status);
	    	return "0";
	    }catch(Exception e){
	    	return "1";
	    }
	}
	/*
	 * 查看配件详情
	 */
	@RequestMapping("/peijianXq")
	public String peijianXq(HttpServletRequest req,HttpServletResponse res,Model model,int goodsId){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	    List<Map<String, Object>> list = indentService.selectpeijianXq(goodsId);
	    model.addAttribute("list",list);
		return "orders/peijianXq";
	}
	/*
	 * 查看商品的单个详情
	 */
	@RequestMapping("/peijianXq2")
	public String peijianXq2(HttpServletRequest req,HttpServletResponse res,Model model,int goodsId){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	    List<Map<String, Object>> list = indentService.selectpeijianXq2(goodsId);
	    model.addAttribute("list",list);
		return "orders/peijianXq2";
	}
	/*
	 * 查看测量详情
	 */
	@RequestMapping("/celiangXq")
	public String celiangXq(HttpServletRequest req,HttpServletResponse res,Model model,int goodsId){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	    List<Map<String, Object>> list = indentService.selectpeijianXq(goodsId);
	    model.addAttribute("list",list);
		return "orders/celiangXq";
	}
	
	/*
	 * 搜索订单
	 */

	@RequestMapping("/search")
	public String toSearch(HttpServletResponse res,Integer indentId,Integer currentPage,Model model){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	    List<Map<String, Object>> list = indentService.selectIndent(model, indentId);
	    model.addAttribute("currentPage",currentPage);
	    model.addAttribute("list",list);
		return "orders/search";
	}

}
